#include "DXUT.h"
#include "PickSystem.h"


CPickSystem::CPickSystem(void)
{
}


CPickSystem::~CPickSystem(void)
{
}

CRay CPickSystem::GetRay( int x,int y )
{
	D3DXMATRIX matView,matProj;
	DXUTGetD3D9Device()->GetTransform(D3DTS_VIEW,&m_matView);
	DXUTGetD3D9Device()->GetTransform(D3DTS_PROJECTION,&m_matProj);
	DXUTGetD3D9Device()->GetViewport(&m_vp9);

	return GetRay(x,y,m_matView,m_matProj,m_vp9);

}

CRay CPickSystem::GetRay( int x,int y,const D3DXMATRIX & matView,const D3DXMATRIX & matProj,const D3DVIEWPORT9 & vp9 )
{

	CRay ray; 


	D3DXMatrixInverse(&matViewInverse,0,&matView);

	float px,py;


	ray.dir.x= (2.0f*x/vp9.Width -1.0f) / m_matProj._11;
	ray.dir.y=(-2.0f*y/vp9.Height+1.0f)/m_matProj._22;
	ray.dir.z=1.0f;



	D3DXVec3TransformCoord(&ray.origin, &ray.origin, &matViewInverse);
	D3DXVec3TransformNormal(&ray.dir, &ray.dir, &matViewInverse);
	D3DXVec3Normalize(&ray.dir, &ray.dir);


	return ray;
}




float CRay::Intersect( const BoundingBox & box )
{

	if(D3DXBoxBoundProbe(&box.min, &box.max, &origin, &dir))
		return D3DXVec3Length(&(((box.min + box.max) / 2.0f) - origin));
	else return -1.0f;
}

float CRay::Intersect( const BoundingSphere & shpere )
{
	if(D3DXSphereBoundProbe(&shpere.centre, shpere.radius, &origin, &dir))
		return D3DXVec3Length(&(shpere.centre - origin));
	else return -1.0f;
}


#pragma region UnitTest
#ifdef _DEBUG

void CPickSystem::UnitTest()
{

	GameUnitTest pUnitTest;
	Singleton<CGame>::Instance()->RunTest(800,600,&pUnitTest,&pUnitTest);

}



void CPickSystem::GameUnitTest::OnUpdate( double fTime, float fElapsedTime )
{

}

void CPickSystem::GameUnitTest::OnTerminate( void )
{
	SAFE_DELETE(m_LineManager);
	SAFE_DELETE(m_Camera);
	Singleton<CGraphicsResourceManager>::Instance()->RemoveAndDestroy(TEXT("cursor"),RESOURCE_TYPE_TEXTURE);
}

HRESULT CPickSystem::GameUnitTest::OnInitialize( void )
{
	Singleton<CGame>::Instance()->AddDeviceStateDeposit(Singleton<CPrimiteRender>::Instance());
	Singleton<CGame>::Instance()->AddRenderDeposit(this);
	Singleton<CGame>::Instance()->AddDeviceStateDeposit(Singleton<CGraphicsResourceManager>::Instance());
    m_pSpriteTexture= new CFileTexture(TEXT("cursor"),TEXT("MouseCursor.dds"));
    Singleton<CGraphicsResourceManager>::Instance()->Add( (void **)&m_pSpriteTexture );
	
	
	m_pHouseMesh = new CMesh(TEXT("House"),TEXT("OwnShip.X"));
	Singleton<CGraphicsResourceManager>::Instance()->Add((void** )&m_pHouseMesh);
 
	m_pHouseMeshInstance =new CMeshInstance(m_pHouseMesh);
 

	m_PlaneMesh =new CAnimMesh(TEXT("plane"),TEXT("plane.X"));

 	Singleton<CGraphicsResourceManager>::Instance()->Add((void** )&m_PlaneMesh);
	m_Camera = new ICamera();

	m_LineManager = new C3DLineManager(20);

	DXUTGetD3D9Device()->SetTransform(D3DTS_PROJECTION,m_Camera->GetProjMatrix());
	m_Cursor =new CSprite(m_pSpriteTexture,D3DXVECTOR3(0,0,0),32,32);

	return S_OK;
}

void CPickSystem::GameUnitTest::OnD3D9FrameRender( float fElapsedTime )
{

	Singleton<CGame>::Instance()->Set2DMode();
	m_Cursor->SetPos(D3DXVECTOR3(Singleton<CInput>::Instance()->GetMouse()->GetMousePos().x,Singleton<CInput>::Instance()->GetMouse()->GetMousePos().y,0));
	m_Cursor->OnD3D9FrameRender(fElapsedTime);
	D3DXMATRIX matWorld;
	D3DXMatrixIdentity(&matWorld);

	DXUTGetD3D9Device()->SetTransform(D3DTS_WORLD,&matWorld);
	DXUTGetD3D9Device()->SetTransform(D3DTS_VIEW,	m_Camera->GetViewMatrix());
	DXUTGetD3D9Device()->SetTransform(D3DTS_PROJECTION,m_Camera->GetProjMatrix());
	DXUTGetD3D9Device()->SetRenderState(D3DRS_LIGHTING,FALSE);
	 


	if(Singleton<CInput>::Instance()->GetMouse()->IsLeftButtonDown())
	{

     	CRay ray= Singleton<CPickSystem>::Instance()->GetRay(Singleton<CInput>::Instance()->GetMouse()->GetMousePos().x ,Singleton<CInput>::Instance()->GetMouse()->GetMousePos().y);
 
		if(ray.Intersect(m_pHouseMeshInstance->GetBoundingBox())!=-1.0f)
		{
			 Singleton<CPrimiteRender>::Instance()->RenderText(TEXT("HouseMesh"),10,10,D3DCOLOR_XRGB(255,255,0));

		}

	}

	DrawBox(m_LineManager,matWorld,m_pHouseMeshInstance->GetBoundingBox(),D3DCOLOR_ARGB(255,255,255,0));
	 DrawSphere( m_pHouseMeshInstance->GetBoundingSphere(),D3DCOLOR_XRGB(0,255,0) );
    
	m_LineManager->OnD3D9FrameRender(fElapsedTime); 
	m_pHouseMeshInstance->SetScale(D3DXVECTOR3(0.01,0.01,0.01));
   m_pHouseMeshInstance->Render();
 
}


#endif // _DEBUG
#pragma endregion UnitTest  //unit test


